/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.misc.dts.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.vacoor.nothing.common.util.IOUtils;
import org.vacoor.nothing.misc.dts.domain.Document;
import org.vacoor.nothing.misc.dts.service.DocumentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * Document Transfer Controller
 *
 * @author vacoor
 */
@Controller
@RequestMapping("/fp")
public class FlexPaperController {
    @Autowired
    private DocumentService documentService;

    @RequestMapping
    public String index(Model model) {
        model.addAttribute("docs", documentService.getDocuments());
        return "index";
    }

    @RequestMapping("cdlnode/ul")
    public void realUpload(String name) {
        String result = "{success: true, fileId: '', version: '', size: 123211}";
    }

    @ResponseBody
    @RequestMapping("ul")
    public String upload(MultipartFile multipart, HttpServletResponse response) throws IOException {
        if (multipart.isEmpty()) {
            response.sendError(403, "Must be upload decode file");
            return "1";
        } else {
            documentService.addDocument(multipart);
            return "0"; // 返回0 上传框才会消失
        }
    }

    @RequestMapping("view")
    public String view(String did, Model model, HttpServletResponse response) throws IOException {
        Document doc = documentService.getDocument(did);
        if (doc == null) {
            response.sendError(404, "File Not Find");
            return null;
        }

        if (Document.Status.SUCCESS != doc.getStatus()) {
            // TODO
        }

        model.addAttribute("doc", doc);
        return "view";
    }

    /**
     * 获取文档给定页的快照
     */
    @RequestMapping("snapshot")
    public void snapshot(@RequestParam("fid") String/*pdfHash*/ pdfHash, @RequestParam(defaultValue = "swf") String format, @RequestParam(defaultValue = "1") int page, HttpServletResponse response) throws IOException, InterruptedException {
        if (!"swf".equalsIgnoreCase(format)) {
            response.sendError(403, "Unsupported file format");
            return;
        }

        InputStream swf = documentService.getSnapshot(pdfHash, page);
        if (swf == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not Find");
        } else {
            /*
            尽可能利用 Cache 减少重复请求, 并发不高情况下应该没问题, 可以在+ cache
            // for http 1.0
            response.setDateHeader("Expires", 0L);
            response.setHeader("progma", "no-cache");
            // http 1.1
            response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
            */

            response.setContentType("application/x-shockwave-flash");
            IOUtils.flow(swf, response.getOutputStream(), true, false);
        }
    }

    @RequestMapping("dl")
    public String download(String did, HttpServletRequest request) {
        Document doc = documentService.getDocument(did);
        return String.format("redirect:/ud/dl/%s?filename=%s", doc.getHash(), doc.getName());
    }
}
